package cn.iocoder.yudao.untils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

public class OrderNumUtil {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
    private static final AtomicInteger INVOICE_COUNTER = new AtomicInteger(1);

    public static synchronized String generateInvoiceNumber() {
        // 获取当前日期
        String datePrefix = DATE_FORMAT.format(new Date());
        // 生成随机的流水号后四位
        int randomSuffix = new Random().nextInt(1000);
        // 使用日期和随机数生成流水号
        String invoiceNumber = datePrefix + String.format("%04d", INVOICE_COUNTER.getAndIncrement() + randomSuffix);
        return invoiceNumber;
    }
}
